﻿#include "fseditrelatedialog.h"
#include "ui_fseditrelatedialog.h"
#include <QSqlQuery>
QString FSeditrelateDialog::ID;
FSeditrelateDialog::FSeditrelateDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FSeditrelateDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("修改信息状态：");
}

FSeditrelateDialog::~FSeditrelateDialog()
{delete ui;}

void FSeditrelateDialog::receiveID(QString id)
{
    QSqlQuery query;
    QString strSql = QString("select * from FileSbmtRow where FileStRowID='%1';").arg(id);
    query.exec(strSql);
    if(query.next()){
        ID=query.value(0).toString();
        ui->edtName->setText(query.value(2).toString());
        if(query.value(3).toInt()<9){
        ui->cbBxIDType->setCurrentIndex(query.value(3).toInt()-1);}
        else{ui->cbBxIDType->setCurrentIndex(7);}
        ui->edtID->setText(query.value(4).toString());
        ui->edtDuty->setText(query.value(5).toString());
        ui->dtTime->setDate(QDate::fromString(query.value(6).toString(),"yyyy-MM-dd"));
        ui->edtMany->setText(query.value(7).toString());
    }
}
void FSeditrelateDialog::on_btnCancel_clicked()
{
    this->close();
}

void FSeditrelateDialog::on_btnOk_clicked()
{
    strName = ui->edtName->text();
    strDuty = ui->edtDuty->text();
    if(ui->cbBxIDType->currentIndex() != 7){
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+1);
    }else{
        strIDType = QString::number(ui->cbBxIDType->currentIndex()+2);
    }
    strID = ui->edtID->text();
    strDutyTime = ui->dtTime->date().toString("yyyy-MM-dd");//任职时间
    rowNum = ui->edtMany->text();

    const char *s = rowNum.toUtf8().data();
    while(*s && *s>='0' && *s<='9')s++;//bool(*s)返回false为数字

    if(strName.trimmed().isEmpty()){
        ui->status->setText("修改信息状态：请输入姓名！");
    }else if(strName.size() > 50){//50
        ui->status->setText("修改信息状态：姓名信息输入有误！");
    }else if(strDuty.trimmed().isEmpty()){
        ui->status->setText("修改信息状态：请输入职务！");
    }else if(strDuty.size() > 80){//80
        ui->status->setText("修改信息状态：职务信息输入有误！");
    }else if(strID.trimmed().isEmpty()){
        ui->status->setText("修改信息状态：请输入证件号！");
    }else if(strID.size() > 18){//18
        ui->status->setText("修改信息状态：证件号信息输入有误！");
    }else if(rowNum.trimmed().isEmpty()){
        ui->status->setText("修改信息状态：请输入卷数！");
    }else if(bool(*s)){
        ui->status->setText("修改信息状态：卷数信息输入有误！");
    }else{
        QString strUpdate=QString("update FileSbmtRow set FilePerName = '%1',CredTypeID = '%2',"
                                  "IDNum = '%3',FilePerDuty = '%4',EmployTime = '%5',"
                                  "FileVolNum = '%6' where FileStRowID = '%9';")
                .arg(strName).arg(strIDType).arg(strID).arg(strDuty)
                .arg(strDutyTime).arg(rowNum).arg(ID);
        QSqlQuery query;
        if(query.exec(strUpdate)){
            ui->status->setText("修改信息状态：信息登记成功！");
            this->close();
        }else{
            ui->status->setText("修改信息状态：信息登记失败！");
        }
    }
}
